package com.itheima.day16反射机制与类加载.反射与类加载.反射类中的属性;

import java.lang.reflect.Field;

public class demo1 {
    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("com.itheima.day16反射机制与类加载.反射与类加载.反射类中的属性.Student");
        Student obj = (Student) clazz.getDeclaredConstructor().newInstance();
        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        System.out.println("反射设置name属性的值");
        name.set(obj, "zhangsan");
        System.out.println("反射获得name属性的值" + (String) name.get(obj));
        Field names = clazz.getField("birthday");
        names.setAccessible(true);
        System.out.println("反射设置birthday属性的值");
        names.set(obj, "1995-10-23");
        System.out.println("反射获得birthday属性的值:" + (String) names.get(obj));
        Field[] fields = clazz.getFields();
        System.out.println("使用:clazz.getFields()方法获取所有属性:");
        for (Field field : fields) {
            System.out.println(field);
            field.setAccessible(true);
            System.out.println("属性:" + field.getName() + "  属性值:" + (String) field.get(obj));
        }
        Field[] df = clazz.getDeclaredFields();
        System.out.println("使用:clazz.getDeclaredFields()方法获取所有公共属性:");
        for (Field field : df) {
            field.setAccessible(true);
            System.out.println("属性:" + field.getName() + "  属性值:" +  field.get(obj));
        }
    }
}
